package com.usian.user.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.usian.model.article.pojos.ApAuthor;
import com.usian.model.base.BaseServiceImpl;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.common.user.RealNameConstans;
import com.usian.model.media.pojos.WmUser;
import com.usian.model.user.dtos.AuthDto;
import com.usian.model.user.pojos.ApUser;
import com.usian.model.user.pojos.ApUserRealname;
import com.usian.user.feign.ArticleFeign;
import com.usian.user.feign.WemediaFeign;
import com.usian.user.mapper.ApUserMapper;
import com.usian.user.mapper.ApUserRealnameMapper;
import com.usian.user.service.ApUserRealnameService;
import io.seata.spring.annotation.GlobalTransactional;
import io.swagger.models.auth.In;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;

@Service
public class ApUserRealnameServiceImpl extends BaseServiceImpl<ApUserRealnameMapper, ApUserRealname> implements ApUserRealnameService {
    @Autowired
    ArticleFeign articleFeign;
    @Autowired
    WemediaFeign wemediaFeign;
    @Autowired
    ApUserMapper apUserMapper;
    @Override
    public ResponseResult loadListByStatus(AuthDto dto) {
        if (dto == null)return errorParam;
        return getPage(dto, Wrappers.<ApUserRealname>lambdaQuery().eq(dto.getStatus()!= null,ApUserRealname::getStatus,dto.getStatus()));
    }

    @Override
    @GlobalTransactional
    public ResponseResult updateByRealId(AuthDto dto) {
        //判断dto是否为空
        if (dto == null)return errorParam;
        //修改当前待审核信息
        ApUserRealname apUserRealname = getById(dto.getId());
        apUserRealname.setStatus(dto.getStatus());
        if (StringUtils.isNotEmpty(dto.getMsg())){
            apUserRealname.setReason(dto.getMsg());
        }
        apUserRealname.setUpdatedTime(new Date());
        updateById(apUserRealname);

        if (dto.getStatus() == RealNameConstans.PASS_AUTH){
            //判断当前自媒体账号是否存在
            WmUser tempWmUser = wemediaFeign.findByName(apUserRealname.getName());
            if (tempWmUser!=null){
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"自媒体账号已存在");
            }
            //创建自媒体账号
            WmUser wmUser = createWmUser(apUserRealname);
            //创建自媒体人
            ResponseResult result = createAuthor(wmUser);
            LinkedHashMap<String,Object> map = (LinkedHashMap<String, Object>) result.getData();
            wmUser.setApAuthorId((Integer) map.get("id"));
            wemediaFeign.updateById(wmUser);
        }
        return successParam;
    }

    private WmUser createWmUser(ApUserRealname realname){
        ApUser apUser = apUserMapper.selectById(realname.getUserId());
        apUser.setFlag((short) 1);
        apUserMapper.updateById(apUser);
        WmUser wmUser = new WmUser();
        BeanUtils.copyProperties(apUser,wmUser);
        wmUser.setCreatedTime(new Date());
        wmUser.setApUserId(apUser.getId());
        wmUser.setType(0);
        wmUser.setStatus(9);
        ResponseResult result = wemediaFeign.save(wmUser);
        LinkedHashMap<String,Object> map = (LinkedHashMap<String, Object>) result.getData();
        wmUser.setId((Integer) map.get("id"));
        return wmUser;
    }
    private ResponseResult createAuthor(WmUser wmUser){
        ApAuthor author = articleFeign.findByUserId(wmUser.getApUserId());
        if (author==null){
            author = new ApAuthor();
            author.setUserId(wmUser.getApUserId());
            author.setCreatedTime(new Date());
            author.setWmUserId(wmUser.getId());
            author.setType(2);
            author.setName(wmUser.getName());
            ResponseResult result = articleFeign.save(author);
            return result;
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"作者已存在");
    }
}